function getstimdata(ExpID)
%% initialize
tmp_data_dir = ImageAnalysis.Settings.TempDataDir;
adata_dir = ImageAnalysis.Settings.AdataDir;

%% find the data file
[adata_file,mouse_id,date_id]=find_adata_file(ExpID,adata_dir);
disp(['___________' num2str(ExpID) '___________'] )

[~,~,stacks]=xlsread(ImageAnalysis.Settings.ExcelFile);
cnt=0;
for ind=1:size(stacks,1)
    if stacks{ind,7}==ExpID
        cnt=cnt+1;
        stacks_ind(cnt)=ind;
    end
end
if exist('stacks_ind','var')
    ExpIDs=stacks(stacks_ind,5);
else
    fprintf(2,'No matching entry in stacks.xls found. Aborting');
    disp(' ')
    return
end
animals=dir(tmp_data_dir);
found_it=zeros(length(ExpIDs),1);
channelFiles ={};
for ind=3:length(animals)
    if sum(found_it)== length(ExpIDs);
        break;
    end
    curr_days=dir([tmp_data_dir '\' animals(ind).name '\ImagingData']);
    for knd=3:length(curr_days)
        if sum(found_it)==length(ExpIDs);
            break;
        end
        curr_files=dir([tmp_data_dir '\' animals(ind).name '\ImagingData\' curr_days(knd).name]);
        for mnd=3:length(curr_files)
            %disp(['file:' curr_files(mnd).name])
            %if sum(found_it)==length(ExpIDs); break; end
            for lnd=1:length(ExpIDs)
                if strcmp(curr_files(mnd).name(4:7),num2str(ExpIDs{lnd}))&&~strcmp(curr_files(mnd).name(end-2:end),'ini')
                    if sum(found_it)==length(ExpIDs)
                        channelFiles = [channelFiles curr_files(mnd).name];
                    else %only update these variables for the first found channel
                        fnames{lnd}=curr_files(mnd).name;
                        mouse_id=animals(ind).name;
                        date_id=curr_days(knd).name;
                        found_it(lnd)=1;
                        channelFiles = [channelFiles curr_files(mnd).name];
                    end
                end
            end
        end
    end
end
for ind=1:length(ExpID)
    aux_files{ind}=[mouse_id stacks{stacks_ind(ind),4} '.lvd'];
    if ~exist([tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{ind}],'file')
        fprintf(2, 'WARNING:')
        disp(['aux file ' aux_files{ind} ' does not exist in here:'])
        disp(['<a href="matlab:winopen(''' tmp_data_dir mouse_id '\Data\' date_id '\' ''')">' tmp_data_dir mouse_id '\Data\' date_id '\' '</a>'])
        return
    end
    stim_files{ind} = [stacks{stacks_ind(ind),11} '.txt'];
    if ~exist([tmp_data_dir mouse_id '\stim\' date_id '\' stim_files{ind}],'file')
        fprintf(2, 'WARNING:')
        disp(['stim file ' aux_files{ind} ' does not exist in here:'])
        disp(['<a href="matlab:winopen(''' tmp_data_dir mouse_id '\stim\' date_id '\' ''')">' tmp_data_dir mouse_id '\stim\' date_id '\' '</a>'])
        return
    end
    
end
if ~isempty(adata_file)
    load([adata_dir mouse_id '\' date_id '\' adata_file])
    %try to get the sync
    stim_data = loadstimdata([tmp_data_dir mouse_id '\stim\' date_id '\' stim_files{ind}]);
    tmp_data=load_lvd([tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{ind}]);
    nFramesDiff=abs(length(get_frame_times(tmp_data(1,:)))-nbr_frames(ind));
    for iAuxChan=1:size(tmp_data,1)
        tmp_frames=get_frame_times(tmp_data(iAuxChan,:));
        frame_channel=iAuxChan;
        if abs(length(tmp_frames)-nbr_frames(ind)) < nFramesDiff
            % found a better match
            nFramesDiff = abs(length(tmp_frames)-nbr_frames(ind));
            bestMatch = iAuxChan;
        end
        if nFramesDiff == 0
            %found a perfect match
            disp(['found frame galvo signal on channel ' num2str(frame_channel)]);
            break
        end
    end
    % sort the stim data put it into bv structure?
    disp(' ')
    if exist('stim_data','var')
        stim_sep=strfind(stim_data.seq, '-');
        if ~isempty(stim_sep)
            
            stim_or = textscan(stim_data.seq(1:stim_sep-1),'%u');
            stim_eye = textscan(stim_data.seq(stim_sep+1:end),'%u');
            stim_or = stim_or{1};
            stim_eye = stim_eye{1};
            
            on=str2num(stim_data.on);
            off=str2num(stim_data.off);
            %    stim_or_nr = uint32(double(stim_or)'./360.*8+1);
            %nr_or = str2num(stim_data.orientations);
            %nr_rep = str2num(stim_data.repetitions);
            %plt_ctr=zeros(2,nr_or);
            %plt_sum=zeros(on+off,2,nr_or);
            orientation = zeros(nbr_frames,1);
            orientation(:) = 0;
            eye = orientation;
            for i=1:nbr_frames/(on+off)
                %plt_ctr(stim_eye(i)+1,stim_or_nr(i)) = plt_ctr(stim_eye(i)+1,stim_or_nr(i)) +1;
                
                %hold on
                %subplot(nr_or, 2, double(stim_eye(i))*double(nr_or)+double(stim_or_nr(i)))
                %hold on
                %plot(ca_activity((i-1)*(on+off)+1:(i)*(on+off)), 'color', [1 plt_ctr(stim_eye(i)+1,stim_or_nr(i))/nr_rep 0])
                %hold on
                %ylim([min(ca_activity) max(ca_activity)]);
                %Splt_sum(:,stim_eye(i)+1,stim_or_nr(i))= plt_sum(:,stim_eye(i)+1,stim_or_nr(i)) + ca_activity((i-1)*(on+off)+1:(i)*(on+off));
                orientation((i-1)*(on+off)+1:(i)*(on+off)) = stim_or(i);
                eye((i-1)*(on+off)+1:(i)*(on+off)) = stim_eye(i);
            end
            bv.orientation = orientation;
            bv.eye = eye;
            assignin('base', 'orientation', orientation);
            assignin('base', 'eye', eye);
            evalin('base','saveadata(adata_dir,ROIs,bv,np,template,dx,dy,aux_files,stim_files,fnames,nbr_frames,mouse_id,date_id,act_map,channelFiles,eye,orientation)');
        else
            fprintf(2, 'Stim format not OK.')
            disp([' '])
        end
    else
        fprintf(2, 'No stim variable stim_data found')
        disp([' '])
    end
    
    if nFramesDiff ~= 0
        fprintf(2,'Stim data does not match aux data!')
        disp(' ')
        
    end
    
else
    fprintf(2,'Adata file does not exist.')
    disp(' ')
end

disp('----Done!----');

%EOF












